package com.net.xpay.common.service.impl;

import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserRegistration;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.UserRegistrationManager;
import com.net.xpay.common.model.UserRegistrationModel;
import com.net.xpay.common.validator.UserRegistrationValidator;
import com.net.xpay.common.service.UserRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserRegistrationServiceImpl implements UserRegistrationService {

    @Autowired
    private UserRegistrationManager userRegistrationManager;

    @Autowired
    private UserManager userManager;

    @Autowired
    private UserRegistrationValidator userRegistrationValidator;

    @Override
    public void submitUserRegistration(UserRegistrationModel model) {
        userRegistrationValidator.validateUserRegistration(model);
        User user = userManager.getById(model.getUserId());

       UserRegistration registration = userRegistrationManager.getByUserId(model.getUserId());
        if (registration != null) {
            registration.setUserId(user.getId());
            registration.setPartnerId(user.getPartnerId());
            registration.setMobile(user.getMobile());
            registration.setBusinessLicense(model.getBusinessLicense());
            registration.setIdCardFront(model.getIdCardFront());
            registration.setIdCardBack(model.getIdCardBack());
            registration.setBizSceneDesc(model.getBizSceneDesc());
            registration.setMerchantCommitment(model.getMerchantCommitment());
            registration.setServiceRatio(model.getServiceRatio());
            userRegistrationManager.update(registration);
        } else {
            UserRegistration userRegistration = new UserRegistration();
            userRegistration.setUserId(user.getId());
            userRegistration.setPartnerId(user.getPartnerId());
            userRegistration.setMobile(user.getMobile());
            userRegistration.setBusinessLicense(model.getBusinessLicense());
            userRegistration.setIdCardFront(model.getIdCardFront());
            userRegistration.setIdCardBack(model.getIdCardBack());
            userRegistration.setBizSceneDesc(model.getBizSceneDesc());
            userRegistration.setMerchantCommitment(model.getMerchantCommitment());
            userRegistration.setServiceRatio(model.getServiceRatio());
            userRegistrationManager.save(userRegistration);
        }
    }
}
